using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace LogFileViewer
{
    public partial class MainWindow : Form
    {

        #region TOOLSTRIP MENU EVENTS

        private void toolStripFileOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog();
        }

        private void toolStripFileClose_Click(object sender, EventArgs e)
        {
            this.listView.VirtualListSize = 0;
            this.listView.VirtualMode = false;
            this.listView.Items.Clear();
            this.data = null;
            this.dataAccess = null;
            UpdateAllEntriesInfoStatusbar();
            this.Text = "LogFileViewer";
        }

        private void toolStripFileExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void toolStripHistoryItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            string filename = (string)item.Tag;

            LoadLogfileNew(filename);
        }
        
        private void toolStripSettingsUserSettings_Click(object sender, EventArgs e)
        {
            UserSettings settingsForm = new UserSettings();
            DialogResult result = settingsForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                LoadWindowSettings();
            }
        }

        private void toolStripSettingsPerformanceSettings_Click(object sender, EventArgs e)
        {
            PerformanceSettings settingsForm = new PerformanceSettings();
            DialogResult result = settingsForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.data = null;
                if (this.dataAccess != null && this.dataAccess.FnProvider != null)
                {
                    LoadLogfileNew(this.dataAccess.FnProvider.OriginalFilename);
                }
            }
        }

        private void toolStripHelpAbout_Click(object sender, EventArgs e)
        {
            AboutBox about = new AboutBox();
            about.ShowDialog();
        }

        #endregion

        #region DIFFERENT EVENTS

        ListViewColumnSorter lvwColumnSorter = new ListViewColumnSorter();

        private void listView_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == lvwColumnSorter.SortColumn)
            {
                if (lvwColumnSorter.Order == SortOrder.Ascending)
                {
                    lvwColumnSorter.Order = SortOrder.Descending;
                }
                else
                {
                    lvwColumnSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                lvwColumnSorter.SortColumn = e.Column;
                lvwColumnSorter.Order = SortOrder.Ascending;
            }
            listView.Sort();
        }

        private void contextMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            int column = (int)item.Tag;
            ChangeColumnWidth(column);
            CreateListViewContextMenuItems();
        }
                
        private void listView_DoubleClick(object sender, EventArgs e)
        {
            if (listView.SelectedIndices.Count != 0)
            {
                // What is selected
                int chosen = listView.SelectedIndices[0];
                ListViewItem item = listView.Items[chosen];
                LogEntry entry = null;
                int elements = 0;
                if (sets.HandleReadMethod == (int)DataAccess.ReaderHandling.ActiveRows)
                {
                    entry = this.RetrieveNeededEntry(chosen);
                    elements = this.listView.VirtualListSize;
                }
                else
                {
                    int columnIndex = (int)item.Tag;
                    string type = item.SubItems[0].Text;
                    entry = this.getData(type)[columnIndex];
                    elements = this.listView.Items.Count;
                }
                if (entry != null)
                {
                    openSingleEntryView(entry, chosen, elements);
                }
                else
                {
                    MessageBox.Show("Cannot open details view", "Error", MessageBoxButtons.OK);
                }
            }
        }

        private void pictureBoxTypeButton_Click(object sender, EventArgs e)
        {
            PictureBox box = (PictureBox)sender;
            int maxSelectTypes = 5;

            string loadThis = null;
            if (this.dataAccess != null && this.dataAccess.FnProvider != null)
            {
                loadThis = this.dataAccess.FnProvider.OriginalFilename;
            }

            if (maxSelectTypes == 1)
            {
                sets.SelectedTypes.Clear();
                string type = "";
                if (btnINFO == box)
                {
                    type = MessageTypes.INFO.ToString();
                }
                else if (btnDEBUG == box)
                {
                    type = MessageTypes.DEBUG.ToString();
                }
                else if (btnWARN == box)
                {
                    type = MessageTypes.WARN.ToString();
                }
                else if (btnERROR == box)
                {
                    type = MessageTypes.ERROR.ToString();
                }
                else if (btnFATAL == box)
                {
                    type = MessageTypes.FATAL.ToString();
                }
                sets.SelectedTypes.Add(type);
                if (loadThis == null)
                {
                    return;
                }
                FileOpenManager manager = new FileOpenManager(this.data);
                manager.ReloadFileContents(this.dataAccess);
                this.data = manager.Data;
            }
            else
            {
                // INFO button check
                if (btnINFO.Image == info2 && btnINFO == box)
                {
                    ActivateType(MessageTypes.INFO);
                }
                else if (btnINFO.Image == info && btnINFO == box && sets.SelectedTypes.Count > 1)
                {
                    DeactivateType(MessageTypes.INFO);
                }

                // DEBUG button check
                if (btnDEBUG.Image == debug2 && btnDEBUG == box)
                {
                    ActivateType(MessageTypes.DEBUG);
                }
                else if (btnDEBUG.Image == debug && btnDEBUG == box && sets.SelectedTypes.Count > 1)
                {
                    DeactivateType(MessageTypes.DEBUG);
                }

                // WARN button check
                if (btnWARN.Image == warn2 && btnWARN == box)
                {
                    ActivateType(MessageTypes.WARN);
                }
                else if (btnWARN.Image == warn && btnWARN == box && sets.SelectedTypes.Count > 1)
                {
                    DeactivateType(MessageTypes.WARN);
                }

                // ERROR button check
                if (btnERROR.Image == error2 && btnERROR == box)
                {
                    ActivateType(MessageTypes.ERROR);
                }
                else if (btnERROR.Image == error && btnERROR == box && sets.SelectedTypes.Count > 1)
                {
                    DeactivateType(MessageTypes.ERROR);
                }

                // FATAL button check
                if (btnFATAL.Image == fatal2 && btnFATAL == box)
                {
                    ActivateType(MessageTypes.FATAL);
                }
                else if (btnFATAL.Image == fatal && btnFATAL == box && sets.SelectedTypes.Count > 1)
                {
                    DeactivateType(MessageTypes.FATAL);
                }
            }
            sets.Save();

            changeButtonStates();

            if (!(sets.HandleReadMethod == (int)DataAccess.ReaderHandling.ActiveRows))
            {
                PopulateListView();
            }

            // Update statusbar "All Entries"
            UpdateAllEntriesInfoStatusbar();
        }

        #endregion

        #region WINDOW EVENTS

        private void MainWindow_Resize(object sender, EventArgs e)
        {
            // Setting window size
            sets.MainWindowWidth = this.Width;
            sets.MainWindowHeight = this.Height;
            sets.Save();
        }

        private void MainWindow_DragOver(object sender, DragEventArgs dea)
        {
            if (dea.Data.GetDataPresent(DataFormats.FileDrop))
            {
                dea.Effect = DragDropEffects.Link;
            }
        }

        private void MainWindow_DragDrop(object sender, DragEventArgs dea)
        {
            if (dea.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])dea.Data.GetData(DataFormats.FileDrop);
                OpenSeveralLogfiles(files);
            }
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            LoadWindowSettings();
        }

        private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveWindowSettings();
        }

        #endregion
    }
}
